var element,layer,upload,form,laydate;
var imgAddress;
var display = 2;

$(document).click(function(){//子页面
    $(".rightmenu",window.parent.document).hide();
});

layui.use(["jquery", "upload", "form", "layer", "element","laydate"], function () {
    $ = layui.$,
        element = layui.element,
        layer = layui.layer,
        upload = layui.upload,
        form = layui.form,
        laydate = layui.laydate;

    laydate.render({
        elem: "#startDate",
        type: "datetime",
        trigger: "click"
    });
    laydate.render({
        elem: "#expireDate",
        type: "datetime",
        trigger: "click"
    });

    //上传图片
    upload.render({
        elem:"#test1",
        url:"/Upload/upload",
        method:"post",
        field:"file",
        accept:"file",
        done:function(res, index, upload){
            if (res.data != null) {
                imgAddress = res.data.substring(res.data);
                localStorage.setItem("imgSrc",imgAddress);
                $(".uploadHeadImage").css("display","block");
                $(".uploadHeadImage img").attr("src",imgAddress);
                layer.msg("上传成功");
            }else{
                layer.msg("上传失败");
            }
        }
    });

    form.on("radio(recommend)",function (data) {
        if (data.value == 2){
            display = 2;
        } else if (data.value == 3){
            display = 3;
        }
    })

    //获取轮播图数据
    var advertisementData = JSON.parse(localStorage.getItem("advertisementData"));
});

//提交轮播图信息
$("#sub").on("click",function () {
    //获取轮播图数据
    var advertisementData = JSON.parse(localStorage.getItem("advertisementData"));
    var user = JSON.parse(localStorage.getItem("user"));
    var userId = user.userId;
    var imgTitle = $("#imgTitle").val();
    var imgType =  $("#imgType").val();
    if (imgType == "0"){
        imgType = '';
    }
    if (imgType == "1"){
        imgType = "视频";
    }
    if (imgType == "2"){
        imgType = "商品";
    }
    var startDate = $("#startDate").val();
    var expireDate = $("#expireDate").val();
    var remarks = $("#remarks").val();
    var carouselId = $("#carouselId").val();

    var data = {
        imgTitle: imgTitle,
        imgType: imgType,
        startDate: startDate,
        expireDate: expireDate,
        remarks: remarks,
        imgAddress: imgAddress,
        createId: userId,
        display: display
    }
    if (imgTitle != '' || startDate != '' || expireDate != '' || imgType != '' || remarks != ''){
        addAdvertisement(data);
    }else {
        layer.msg("请补充信息");
    }
    return false;
});

function addAdvertisement(data) {
    $.ajax({
        url:'/Advertisement/addAdvertisement',
        methods: "post",
        dataType:'json',
        data: data,
        success:function (data) {
            if (data.data){
                //清空页面显示数据
                $("#imgTitle").val('');
                $("#imgType").val(0);
                $("#startDate").val('');
                $("#expireDate").val('');
                $("#remarks").val('');
                $(".uploadHeadImage").css("display","none");
                $(".uploadHeadImage img").attr("src","");
                //重新渲染表单
                form.render();
                layer.msg("添加成功");
                //轮播图添加成功后刷新轮播图列表页面
                parent.$('#quickEntry').val('轮播图列表');
                parent.quickEntry();
            }else{
                layer.msg("添加失败");
            }
        }
    });
}

//返回
function backOff() {
    var arr = localStorage.getItem("numArr");
    var numArr = arr.split(",");
    numArr.pop();
    //跳转父窗口指定选项卡页面
    window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
    localStorage.setItem("numArr",numArr);
}